package com.example.smartcity_40.tool

import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import androidx.viewpager.widget.PagerAdapter
import com.example.smartcity_40.MainActivity
import com.example.smartcity_40.R
import com.example.smartcity_40.dialog.CustomDialog

class IntroductionAdapter(private val icons: IntArray, val context: Context) : PagerAdapter() {

    override fun getCount(): Int {
        return icons.size
    }

    override fun isViewFromObject(p0: View, p1: Any): Boolean {
        return p0 == p1
    }

    override fun instantiateItem(container: ViewGroup, position: Int): Any {
        val view =
                LayoutInflater.from(container.context).inflate(R.layout.item_intro_duction, container, false)
        val nextButton: Button = view.findViewById(R.id.nextButton)
        val dialogShow: Button = view.findViewById(R.id.dialog_show)
        val guideImage: ImageView = view.findViewById(R.id.pageIcon)
        guideImage.setImageResource(icons[position])
        if (position == getCount() - 1) {
            nextButton.visibility = View.VISIBLE
            dialogShow.visibility = View.VISIBLE
            dialogShow.setOnClickListener {
                CustomDialog(context).show()
            }
            nextButton.setOnClickListener {
                val intent = Intent(context, MainActivity::class.java)
                context.startActivity(intent)
            }
        } else {
            dialogShow.visibility = View.GONE
            nextButton.visibility = View.GONE
        }
        container.addView(view)
        return view
    }

    override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
        container.removeView(`object` as View)
    }
}